package cn.controller;

import cn.entity.Demand;
import cn.entity.Result;
import cn.service.DemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

@RestController
@RequestMapping("/demand")
public class DemandController {

    @Autowired
    DemandService demandService;
    @RequestMapping("index")
    public ModelAndView index(){
        return new ModelAndView("app/index");
    }
    @RequestMapping("/index/{pageNum}/{pageSize}")
    public Result selectPage(@PathVariable("pageNum")int pageNum,@PathVariable("pageSize")int pageSize){
        return new Result(true,"成功",demandService.selectPage(pageNum,pageSize));
    }
    @RequestMapping("toDetail")
    public ModelAndView toDetail(){
        return new ModelAndView("demand/detail");
    }

    @RequestMapping("/showDetail/{id}")
    public Demand showDetail(@PathVariable("id")Long id){
        return demandService.selectByPrimaryKey(id);
    }
    @RequestMapping("/toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("demand/update");
    }
    @RequestMapping("/showUpdate/{uid}")
    public Demand showUpdate(@PathVariable("uid")Long uid){
        return demandService.selectByPrimaryKey(uid);
    }
    @RequestMapping("doUpdate")
    public int doUpdate(@RequestBody Demand demand){
        demand.setUpdateDate(new Date());
        return demandService.updateByPrimaryKeySelective(demand);
    }
}
